define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    
    require('../su/accordion/oaTabCtrl');
    
    $.widget('rk.colleagueDepartContentCtrl', $.rk.oaContentCtrl, {
        _create: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var temp = require('oa/tmpl/colleague/content/tpl_colleague_depart_content.tpl');
            var html = temp;
            elem.html(rk.templateText(html));
            elem.find("[act='colleague_depart_follow']").beTip();
            elem.find("[act='colleague_depart_content_tab']").tabs({
                active: 0,
                activate: function(e, ui) {
                    $.proxy(me.showDepartContentTabs(e, ui), me);
                }
            });
            var divs = $("#colleague_depart_content_index_tab");
            me.loaddata(opt.type, {}, divs);
            me._listeners();
        },
        _listeners: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (opt.type === "depart") {
                me._depart_content_listeners();
            } else if (opt.type === "colleague") {
                me._colleague_content_listeners();
            }
        },
        _depart_content_listeners: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            elem.on("click", "[act='colleague_depart_set_button']", function() {
                require.async(["../group/content/groupSetCtrl.js"], function() {
                    rk.dialog({
                        title: rk.i18n('COLLEAGUE_SETUP'),
                        success: function(contentElem) {
                            contentElem.groupSetCtrl({
                                gid: opt.gid,
                                type: "depart"
                            }); //初始化弹框内容的Ctrl
                        },
                        footer: ['cancel', {
                            act: 'ok',
                            visible: true,
                            text: rk.i18n('COLLEAGUE_SAVE')
                        }]
                    });
                });
            });
            elem.on("click", "[act='colleague_depart_follow']", function() {
                var button = $(this);
                var isFollow = button.attr("isFollow");
                if (button.hasClass('disable')) return;
                button.addClass('disable');
                if (isFollow == "1") {
                    rk.confirm(rk.i18n('COLLEAGUE_CONFIRM_UNFOLLOW'), function() {
                        rk.post("/json/oa_group/member-delfollow.action", {
                            gid: opt.gid
                        }, function(json) {
                            if (json.status === 0) {
                                button.addClass("no_follow");
                                button.removeClass("has_follow");
                                button.attr("isFollow", "0");
                                button.attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            } else if (json.status === 217002) {
                                rk.noticeError(rk.i18n('COLLEAGUE_GROUP_BELONG_ERROR'));
                            } else {
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                            button.removeClass('disable');
                        });
                    });
                } else if (isFollow == "0") {
                    rk.post("/json/oa_group/member-follow.action", {
                        gid: opt.gid
                    }, function(json) {
                        if (json.status === 0) {
                            button.removeClass("no_follow");
                            button.addClass("has_follow");
                            button.attr("isFollow", "1");
                            button.attr("title", rk.i18n('COLLEAGUE_CANCEL_FOLLOW'));
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        } else if (json.status == 217002) {
                            rk.noticeError(rk.i18n('COLLEAGUE_GROUP_BELONG_ERROR'));
                        } else {
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                        button.removeClass('disable');
                    });
                }
            });
            me._depart_content_index_listener();
        },
        _depart_content_index_listener: function() {
            //colleague_depart_detail_open
            var me = this;
            var elem = me.element;
            //elem.on("click", "[act='colleague_depart_detail_open']", function(){
            //    var button = $(this);
            //    var gid = button.attr("gid");
            //    rk.oa.popupFinal({param: {gid:gid,type:"depart"}, ctrlPath: 'oa/js/colleague/colleagueDepartContentCtrl'})
            //});
        },
        showDepartContentTabs: function(e, ui) {
            var me = this;
            var elem = me.element; //loadDataType
            if (ui) {
                var divs = ui.newPanel;
                me.loaddata(divs.attr("loadDataType"), {}, divs);
            }
        },
        _init: function() {
            var me = this;
            var elem = me.element;
            elem.hide();
        },
        loaddata: function(type, param, divs) {
            var me = this;
            var elem = me.element;
            if (!param) {
                param = {
                    pageNo: 1,
                    pageSize: 20
                };
            }
            if (type && me.loadDataJson[type]) {
                $.proxy(me.loadDataJson[type].call(this, param, divs), me);
            }
        },
        loadDataJson: {
            "depart": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                divs.beLoading();
                rk.post("/json/oa_group/depart-detail.action", {
                    gid: opt.gid
                }, function(json) {
                    if (json.status === 0 || json.status == 300001) {
                        elem.show();
                        if(json.depart && json.depart.id) {
                            var temp = require('oa/tmpl/colleague/content/tpl_colleague_depart_content_index.tpl');
                            var html = rk.templateText(temp, json.depart);
                            elem.find("[act='colleague_depart_name']").html(json.depart.name);
                            if (divs) {
                                divs.html(html);
                                require.async(["core/apps/feed/feed"], function () {
                                    var arCallbacks = [];
                                    var height;
                                    if(elem.parent().parent() && elem.parent().parent().parent().attr("act") == "oacontent"){
                                        height = elem.parent().parent().height();
                                    }else{
                                        height = $("#rk-panel").height() - 50;
                                    }
                                    elem.height(height);
                                    elem.beScroll({
                                        whileScrolling: function() {
                                            var scrollTop = Math.abs(parseInt(elem.find('.mCSB_container').css('top'), 10));
                                            if (scrollTop > elem.closest('.mCustomScrollbar').height()) {
                                                divs.find('a.js-gototop').show();
                                            } else {
                                                divs.find('a.js-gototop').hide();
                                            }
                                        },
                                        onTotalScroll: function() {
                                            for (var i = 0, len = arCallbacks.length; i < len; i++) {
                                                var fn = arCallbacks[i];
                                                if ($.isFunction(fn)) {
                                                    fn();
                                                }
                                            }
                                        },
                                        onTotalScrollOffset: 120,
                                        alwaysTriggerOffsets: true
                                    });
                                    divs.find("[act='colleague_depart_message']").feed2015({
                                        scrollRegister: function(fn) {
                                            arCallbacks.push(fn);
                                        },
                                        tabs: [{
                                            tabname: 'none',
                                            prependnew: 'true',
                                            url: '/json/sns_feed/groupFeeds.action?gid=' + opt.gid,
                                            appendmore: 'true'
                                        }],
                                        hasEditor: true,
                                        groupId: opt.gid
                                    });
                                });
                            }
                            if (json.depart.isGroupAdmin) { //colleague_depart_set_div
                                elem.find("[act='colleague_depart_set_div']").show();
                            } else {
                                elem.find("[act='colleague_depart_set_div']").hide();
                            }

                            if (json.depart.isGroupMember) {
                                elem.find("[act='colleague_depart_follow_div']").show();
                            } else {
                                elem.find("[act='colleague_depart_follow_div']").hide();
                            }

                            if (json.depart.isGroupFollow) {
                                elem.find("[act='colleague_depart_follow']").removeClass("no_follow");
                                elem.find("[act='colleague_depart_follow']").addClass("has_follow");
                                elem.find("[act='colleague_depart_follow']").attr("isFollow", "1");
                                elem.find("[act='colleague_depart_follow']").attr("title", rk.i18n('COLLEAGUE_CANCEL_FOLLOW'));
                            } else {
                                elem.find("[act='colleague_depart_follow']").addClass("no_follow");
                                elem.find("[act='colleague_depart_follow']").removeClass("has_follow");
                                elem.find("[act='colleague_depart_follow']").attr("isFollow", "0");
                                elem.find("[act='colleague_depart_follow']").attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                            }
                        } else if(!json.canAccess){
                            var temp = require("oa/tmpl/colleague/tpl_error_colleague.tpl");
                            var html = rk.templateText(temp, {
                                text: rk.i18n('COLLEAGUE_NO_DEPART_AUTHORITY_ERROR')
                            });
                            elem.html(html);
                        }else{
                            var temp = require("oa/tmpl/colleague/tpl_error_colleague.tpl");
                            var html = rk.templateText(temp, {
                                text: rk.i18n('COLLEAGUE_NO_DEPART_ERROR')
                            });
                            elem.html(html);
                        }
                    } else {
                        rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    }
                });
            },
            "depart_member": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                //param.gid = opt.gid;
                divs.beLoading();
                require.async(["./content/colleagueDepartMemberTabCtrl.js"], function() {
                    divs.colleagueDepartMemberTabCtrl({
                        gid: opt.gid
                    });
                });
            },
            "depart_file": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                require.async(["../group/content/groupContentFileCtrl.js"], function() {
                    divs.groupContentFileCtrl({
                        gid: opt.gid,
                        type: "depart"
                    });
                });
            },
            "depart_blog": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                require.async(["../blog/blogTableCtrl.js"], function() {
                    divs.blogTableCtrl({
                        type: "departBlog",
                        gid: opt.gid
                    });
                });
            }
        }
    });
    return {
        init: function(rkContent, id) {
            rkContent.colleagueDepartContentCtrl({
                gid: id,
                type: "depart"
            });
        }
    }
});
